package com.shop.member.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shop.common.constant.Constants;
import com.shop.common.utils.uuid.UUID;
import net.sf.jsqlparser.Model;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.shop.common.annotation.Log;
import com.shop.common.core.controller.BaseController;
import com.shop.common.core.domain.AjaxResult;
import com.shop.common.enums.BusinessType;
import com.shop.member.domain.ShopUser;
import com.shop.member.service.IShopUserService;
import com.shop.common.utils.poi.ExcelUtil;
import com.shop.common.core.page.TableDataInfo;
import org.springframework.web.servlet.ModelAndView;

/**
 * 会员Controller
 *
 * @author hao_wu
 * @date 2022-04-04
 */
@RestController
@RequestMapping("/member/member")
public class ShopUserController extends BaseController {
    @Autowired
    private IShopUserService shopUserService;

    /**
     * 查询会员列表
     */
    @PreAuthorize("@ss.hasPermi('member:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(ShopUser shopUser) {
        startPage();
        List<ShopUser> list = shopUserService.selectShopUserList(shopUser);
        return getDataTable(list);
    }

    /**
     * 导出会员列表
     */
    @PreAuthorize("@ss.hasPermi('member:member:export')")
    @Log(title = "会员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShopUser shopUser) {
        List<ShopUser> list = shopUserService.selectShopUserList(shopUser);
        ExcelUtil<ShopUser> util = new ExcelUtil<ShopUser>(ShopUser.class);
        util.exportExcel(response, list, "会员数据");
    }

    /**
     * 获取会员详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(shopUserService.selectShopUserById(id));
    }

    /**
     * 手机号查重
     */
    @PutMapping(value = "/register")
    public AjaxResult getInfo(ShopUser shopUser) {
        return AjaxResult.success(shopUserService.register(shopUser));
    }

    /**
     * 新增会员
     */
    @Log(title = "会员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShopUser shopUser) {
        return toAjax(shopUserService.insertShopUser(shopUser));
    }

    /**
     * 修改会员
     */
    @PreAuthorize("@ss.hasPermi('member:member:edit')")
    @Log(title = "会员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShopUser shopUser) {
        return toAjax(shopUserService.updateShopUser(shopUser));
    }

    /**
     * 删除会员
     */
    @PreAuthorize("@ss.hasPermi('member:member:remove')")
    @Log(title = "会员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(shopUserService.deleteShopUserByIds(ids));
    }

    /**
     * 会员登录
     */
    @PostMapping("/login")
    //登录功能
    public AjaxResult login(ModelMap modelMap, ShopUser shopUser, HttpSession session) {
        ShopUser user = shopUserService.login(shopUser);
        if (user != null) {
            //登录成功
            String token = UUID.randomUUID().toString();
            modelMap.put("user", user);
            return AjaxResult.success(user);
        }
        return AjaxResult.error("账号或者密码错误");
        //error
    }
}
